package backstage.sys.listener;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PlatformLoaderServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private PlatformLoader platformLoader;

	public void init() throws ServletException {
		this.platformLoader = createPlatformLoader();
		this.platformLoader.init(getServletContext());
	}

	/**
	 * 创建PlatformLoader使用，可以被子类覆盖.
	 * 
	 * @return the new ContextLoader
	 */
	protected PlatformLoader createPlatformLoader() {
		return new PlatformLoader();
	}

	public PlatformLoader getPlatformLoader() {
		return platformLoader;
	}

	public void destroy() {
		if (this.platformLoader != null) {
			this.platformLoader.getContextLoader().closeWebApplicationContext(
					getServletContext());
		}
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		getServletContext().log(
				"Attempt to call service method on ContextLoaderServlet as ["
						+ request.getRequestURI() + "] was ignored");
		response.sendError(HttpServletResponse.SC_BAD_REQUEST);
	}

	public String getServletInfo() {
		return "ContextLoaderServlet for Servlet API 2.2/2.3 "
				+ "(deprecated in favor of ContextLoaderListener for Servlet API 2.4)";
	}

}